<?php

/* All the useful classes in my daily work.
 * ========================================
 * Lists:
 * 1. DB: A DB class singleton pattern
 * 2. 
 */


/* DB class singleton pattern
 * How to use:
 * $stmt = DB::prepare('select id, name from users');
 * $stmt->execute();
 * DB::setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 */
class DB {

    // update the db config upon your environment
    private static $dsn = 'mysql:host=localhost;dbname=ll';
    private static $dbuser = 'll';
    private static $dbpass = 'snriud';

    private static $instance = null;

    private function getInstance() {
        if (self::$instance) {
            return self::$instance;
        }

        self::$instance= new PDO(self::$dsn, self::$dbuser, self::$dbpass);
        self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
        self::$instance->setAttribute(PDO::ATTR_PERSISTENT, true);
        
        return self::$instance;
    }

    public static function __callStatic ($name, $args) {
        $callback = array(self::getInstance(), $name);
        return call_user_func_array($callback, $args);
    }
}
